package sheshen.come.cn.sheshen;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import sheshen.come.cn.sheshen.bell.BellFragment;
import sheshen.come.cn.sheshen.chat.ChatFragment;
import sheshen.come.cn.sheshen.feature.FeaturesFragment;
import sheshen.come.cn.sheshen.mine.MineFragment;
import sheshen.come.cn.sheshen.navigation.imp.DefaultNavigation;

public class MainActivity extends AppCompatActivity implements TabHost.OnTabChangeListener{
    //今年拟解决的肌肤
    private List<TabItem> tabItemList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initTabData();
        initTabHost();
    }

    private void initTabData(){
        tabItemList = new ArrayList<TabItem>();
        //消息
        tabItemList.add(new TabItem(R.mipmap.chat_normal
                ,R.mipmap.chat_state,R.string.main_chat_text, ChatFragment.class));
        //提醒
        tabItemList.add(new TabItem(R.mipmap.bell_normal
                ,R.mipmap.bell_state,R.string.main_bell_text, BellFragment.class));
        //功能
        tabItemList.add(new TabItem(R.mipmap.keypad_normal
                ,R.mipmap.keypad_state,R.string.main_features_text, FeaturesFragment.class));
        //添加我的Tab
        tabItemList.add(new TabItem(R.mipmap.user_normal
                ,R.mipmap.user_state,R.string.main_mine_text, MineFragment.class));
    }
    //初始化主页选项卡视图
    private void initTabHost(){
        //获取FragmentTabHost
        FragmentTabHost fragmentTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        //绑定TabHost(绑定我们的body)
        fragmentTabHost.setup(this,getSupportFragmentManager(),android.R.id.tabcontent);
        //去掉分割线
        fragmentTabHost.getTabWidget().setDividerDrawable(null);
        for (int i  = 0;i<tabItemList.size();i++){
            TabItem tabItem = tabItemList.get(i);
            //绑定Fragment(将Fragment添加到FragmentTabHost组件上面)
            //newTabSpec:代表Tab名字
            //setIndicator:图片(今天我们采用布局文件--Tab到样式我们自己做)
            TabHost.TabSpec tabSpec = fragmentTabHost.
                    newTabSpec(tabItem.getTitleString())
                    .setIndicator(tabItem.getView());
            //将底部的图片和fragment关联起来;tabItem.getBundle():给我们的具体的Fragment传参数
            fragmentTabHost.addTab(tabSpec,tabItem.getFragmentClass(),tabItem.getBundle());
            //给我们的Tab按钮设置背景
            fragmentTabHost.getTabWidget()
                    .getChildAt(i)
                    .setBackgroundColor(getResources().getColor(R.color.main_bottom_bg));
            //默认选中第一个Tab
            if (i == 0){
                tabItem.setChecked(true);
            }
            //设置切换监听
            fragmentTabHost.setOnTabChangedListener(this);
        }
    }

    @Override
    public void onTabChanged(String tabId) {
        for (int i = 0;i < tabItemList.size();i++){
            TabItem tabItem = tabItemList.get(i);
            if (tabId.equals(tabItem.getTitleString())){
                //选中设置为选中壮体啊
                tabItem.setChecked(true);
            }else {
                //没有选择Tab样式设置为正常
                tabItem.setChecked(false);
            }
        }
    }

    class TabItem{
        private int imageNormal;
        private int imagePress;
        private int title;
        private String titleString;
        private Bundle bundle;
        //
        private Class<? extends Fragment> fragmentClazz;
        private View view;
        private TextView textView;
        private ImageView imageView;
        public TabItem(int normal, int imagePress, int title,Class<? extends Fragment> fragmentClazz) {
            this.imageNormal = normal;
            this.imagePress = imagePress;
            this.title = title;
            this.fragmentClazz = fragmentClazz;
        }
        public int getImageNormal() {
            return imageNormal;
        }

        public int getImagePress() {
            return imagePress;
        }

        public int getTitle() {
            return title;
        }

        public Class<? extends Fragment> getFragmentClass() {
            return fragmentClazz;
        }
        public String getTitleString() {
            if (title == 0){
                return "";
            }
            if (TextUtils.isEmpty(titleString)){
                titleString = getString(title);
            }
            return titleString;
        }

        //切换table
        //还需要提供一个切换Tab方法---改变Tab样式
        public void setChecked(boolean isChecked){
            if(imageView != null){
                if (isChecked){
                    imageView.setImageResource(imagePress);
                }else {
                    imageView.setImageResource(imageNormal);
                }
            }
            if (textView != null && title != 0){
                if (isChecked){
                    textView.setTextColor(getResources().getColor(R.color.main_bottom_text_select));
                }else {
                    textView.setTextColor(getResources().getColor(R.color.main_bottom_text_normal));
                }
            }
        }
        public View getView(){
            if (this.view==null){
                this.view = getLayoutInflater().inflate(R.layout.view_tab_indicator,null);
                this.imageView = (ImageView)this.view.findViewById(R.id.iv_tab);
                this.textView = (TextView) this.view.findViewById(R.id.tv_tab);
                //判断资源是否存在,不再我就因此
                if (this.title == 0){
                    this.textView.setVisibility(View.GONE);
                }else {
                    this.textView.setVisibility(View.VISIBLE);
                    this.textView.setText(getTitleString());
                }
                //绑定图片默认资源
                this.imageView.setImageResource(imageNormal);
            }
            return this.view;
        }
        public Bundle getBundle(){
            if (bundle == null){
                bundle = new Bundle();
            }
            bundle.putString("title",getTitleString());
            return bundle;
        }
    }

}